11 const double infinity
= 1E20
;
15 point(double X
, double Y
){ x
= X
; y
= Y
;}
18 map
< point
, double > dist
;
20 bool operator ==(const point
&a
, const point
&b
){ return (a
.x
== b
.x
&& a
.y
== b
.y
);}
21 bool operator !=(const point
&a
, const point
&b
){ return !(a
== b
);}
22 bool operator <(const point
&a
, const point
&b
){ return (a
.x
< b
.x
|| (a
.x
== b
.x
&& a
.y
< b
.y
));}
23 //bool operator <(point a, point b){ return (a.dist > b.dist);}
24 double distancia(point a
, point b
){return hypot(a
.y
-b
.y
, a
.x
-b
.x
);}
28 bool heapCmp(const point
&a
,const point
&b
){
29 return (dist
[a
] > dist
[b
]);
33 struct heapCompare
: public binary_function
<point
, point
, bool>
35 bool operator()(const point
&x
, const point
&y
) const
36 { return dist
[x
] > dist
[y
]; }
41 //contiene todos los nodos sueltos
43 //contiene un vector con todos los vecinos para el punto point
44 map
< point
, vector
<point
> > vecinos
;
47 if (vecinos
.count(a
)) return;
49 if (vecinos
.size() == 0){
51 vecinos
.insert(make_pair(a
, v
));
54 //printf("vecinos.begin() apunta a (%f,%f)\n", (*vecinos.begin()).first.x, (*vecinos.begin()).first.y);
56 map
< point
, vector
<point
> >::iterator lower
, upper
;
57 lower
= vecinos
.lower_bound(point(a
.x
-1.5, a
.y
-1.5));
58 //if (lower == vecinos.begin()) printf("Lower == vecinos.begin() que apunta a (%f,%f)\n", (*vecinos.begin()).first.x, (*vecinos.begin()).first.y);
59 //if (lower == vecinos.end()) printf("Lower == vecinos.end()\n");
61 if (lower
!= vecinos
.begin()) --lower
;
62 upper
= vecinos
.upper_bound(point(a
.x
+1.5, a
.y
+1.5));
64 printf("Todos los vecinos de (%f,%f) deben estar entre: (%f,%f) y (%f,%f)\n", a
.x
, a
.y
,
65 (*lower
).first
.x
, (*lower
).first
.y
, (*upper
).first
.x
, (*upper
).first
.y
);
69 map
< point
, vector
<point
> >::iterator it
;
70 for (it
=lower
; it
!= upper
; ++it
){
71 //Insertarlo como vecino en los nodos diferentes a el mismo
72 if ((*it
).first
!= a
){
73 vector
<point
> adj
= vecinos
[(*it
).first
];
74 //Asegurarse de no insertar un vecino repetido
75 if (distancia(a
, (*it
).first
) <= 1.5){
76 //printf("Insertando (%f,%f) como vecino de (%f,%f).\n", a.x, a.y, nodos[i].x, nodos[i].y);
77 vecinos
[(*it
).first
].push_back(a
);
78 vecinos
[a
].push_back((*it
).first
);
86 for (int i
=0; i
<nodos
.size(); ++i
){
87 dist
[nodos
[i
]] = infinity
;
88 if (nodos
[i
].x
== 0.00 && nodos
[i
].y
== 0.00){
89 dist
[nodos
[i
]] = 0.00;
94 /*void relax(point u, point v){
95 if (dist[v] > dist[u] + distancia(u, v)){
96 dist[v] = dist[u] + distancia(u, v);
100 void dijkstra(const double &maxPath
, const point
&finalPoint
){
104 priority_queue
<point
, vector
<point
>, heapCompare
> q
;
105 q
.push(point(0.0, 0.0));
112 if (distancia(point(0.00, 0.00), u
) + distancia(u
, finalPoint
) <= maxPath
){
113 for (int i
=0; i
<vecinos
[u
].size(); ++i
){
114 point v
= vecinos
[u
][i
];
115 //printf(" Comparando (%f,%f) con (%f,%f) que estan a %f\n", u.x, u.y, v.x, v.y, distancia(u,v));
116 //printf(" dist[u] es %f, dist[v] es %f\n", dist[u], dist[v]);
117 if (dist
[vecinos
[u
][i
]] > (dist
[u
] + distancia(u
,v
))){
118 //printf(" Actualizando la distancia de v = (%f,%f)\n", v.x, v.y);
119 dist
[vecinos
[u
][i
]] = dist
[u
] + distancia(u
, v
);
120 //printf(" Nueva distancia de v: %f\n", dist[v]);
129 printf("Voy a imprimir los elementos del mapa en orden.\n");
130 map
< point
, vector
<point
> >::iterator i
;// = vecinos.begin();
131 for (i
= vecinos
.begin(); i
!= vecinos
.end(); ++i
){
132 printf(" (%f,%f)\n", (*i
).first
.x
, (*i
).first
.y
);
145 for (s
= ""; s
== ""; getline(cin
, s
));
156 g
.insert(point(0.00, 0.00));
157 g
.insert(point((double)w
, (double)h
));
160 for (int i
=0; i
<noPuntos
; ++i
){
163 g
.insert(point(x
,y
));
167 cout
<< "Termine de insertar todos los nodos.\n";
175 /*printf("Voy a imprimir el grafo de %d nodos:\n", g.nodos.size());
176 for (int i=0; i<g.nodos.size(); ++i){
177 printf("Estos son los vecinos de (%f,%f):\n", g.nodos[i].x, g.nodos[i].y);
178 for (int j=0; j<g.vecinos[g.nodos[i]].size(); ++j){
179 printf(" (%f,%f)\n", g.vecinos[g.nodos[i]][j].x, g.vecinos[g.nodos[i]][j].y);
183 g
.dijkstra(maximoCamino
, point((double)w
, (double)h
));
184 //printf("La distancia minima hasta (%d,%d) es %f.\n", w,h,dist[point((double)w, (double)h)]);
185 if (dist
[point((double)w
, (double)h
)] <= maximoCamino
){
186 printf("I am lucky!\n");